package com.zhan.ielts.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.CustomResult;
import com.zhan.ielts.domain.Users;
import com.zhan.ielts.domain.VocabularyGroup;
import com.zhan.ielts.domain.query.VocabularyGroupQuery;
import com.zhan.ielts.service.UsersService;
import com.zhan.ielts.service.VocabularyGroupService;

@Controller
@RequestMapping("/vocabulary/group")
public class VocabularyGroupController extends BaseControllerImpl<VocabularyGroup, VocabularyGroupQuery> {

	private Logger log = LoggerFactory.getLogger(UnknownVocabularyController.class);

	@Autowired
	private VocabularyGroupService vocabularyGroupService;

	@Autowired
	private UsersService usersService;

	@Override
	protected BaseService<VocabularyGroup> getBaseService() {

		return vocabularyGroupService;
	}

	@ResponseBody
	@RequestMapping(value = "/fetch", method = RequestMethod.POST)
	public CustomResult fetchVocabularyGroup(VocabularyGroupQuery data) {
		log.debug("获取生词本分类信息开始");
		Map<String, Integer> map = new HashMap<String, Integer>();

		// 检查userId 是否为空
		if (StringUtils.isEmpty(data.getUserId())) {
			map.put("errCode", 1001);
			return new CustomResult(0, "userId为空！", map);
		}
		List<VocabularyGroup> vocabularyGroupList = null;

		try {
			// 检查userId 是否有效
			Users query = new Users();
			query.setId(data.getUserId());
			if (usersService.queryCount(query) < 1) {
				map.put("errCode", 1002);
				return new CustomResult(0, "userId无效！", map);
			}

			vocabularyGroupList = vocabularyGroupService.fetchVocabularyGroup(data);

		} catch (Exception e) {
			map.put("errCode", 1003);
			return new CustomResult(0, "系统错误！", map);
		}
		log.debug("获取生词本分类信息结束");

		return new CustomResult(1, "", vocabularyGroupList);
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public CustomResult updateVocabularyGroup(String userId, String newWordCategory, String device, String osVersion, String appVersion) {
		log.debug("更新生词本分类开始");
		Map<String, Integer> map = new HashMap<String, Integer>();

		// 检查userId 是否为空
		if (userId == null || userId.trim().equals("")) {
			map.put("errCode", 1001);
			return new CustomResult(0, "userId为空！", map);
		}

		// 检查userId 是否有效
		Users query = new Users();
		query.setId(userId);
		if (usersService.queryCount(query) < 1) {
			map.put("errCode", 1002);
			return new CustomResult(0, "userId无效！", map);
		}

		try {

			JSONArray jan = JSONArray.fromObject(newWordCategory);
			List<VocabularyGroup> vocabularyGroupList = JSONArray.toList(jan, VocabularyGroup.class);

			vocabularyGroupService.updateVocabularyGroup(vocabularyGroupList, userId, device, osVersion, appVersion);
		} catch (Exception e) {
			map.put("errCode", 1003);
			return new CustomResult(0, "系统错误！", map);
		}

		log.debug("更新生词本分类结束");

		return new CustomResult(1, "", map);
	}

	@Override
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView addOne(VocabularyGroup entity) {
		// entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		try {
			vocabularyGroupService.insert(entity);
		} catch (Exception e) {
			return new ModelAndView(path.getRedirectListPath());
		}
		return new ModelAndView(path.getRedirectListPath());
	}

	@Override
	@RequestMapping(method = RequestMethod.PUT)
	public ModelAndView editOne(VocabularyGroup entity) {
		// entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		vocabularyGroupService.updateById(entity);
		return new ModelAndView(path.getRedirectListPath());
	}

}
